package genedit.gui;

import genedit.model.Workspace;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

public class GFileChooser extends JFileChooser {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static GFileChooser instance;
	private static FileNameExtensionFilter projFilter=new FileNameExtensionFilter("GRED projects (.proj)", "proj");
	private static FileNameExtensionFilter xmlFilter=new FileNameExtensionFilter("XML document (.xml)", "xml");
	
	private GFileChooser() {
		super();
		if (Workspace.getInstance().getPath() != null)
			setCurrentDirectory(Workspace.getInstance().getPath());
		setAcceptAllFileFilterUsed(false);
	}

	public static GFileChooser getInstance(boolean onlyDirectories) {
		if (instance == null)
			instance = new GFileChooser();
		if(onlyDirectories){
			FileFilter[] filters=instance.getChoosableFileFilters();
			for(FileFilter filter:filters){
				instance.removeChoosableFileFilter(filter);
			}
			
		}else{
			instance.addChoosableFileFilter(projFilter);
			instance.addChoosableFileFilter(xmlFilter);
		}
		
		return instance;
	}
}
